﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Statistics
{
    /// <summary>
    /// File size format helper
    /// </summary>
    /// 
    /// <author>Alexander Seeliger </author>
    public class FileSizeFormat
    {
        /// <summary>
        /// Convert units
        /// </summary>
        private static readonly long kilobyte = 1024;
        private static readonly long megabyte = 1024 * kilobyte;
        private static readonly long gigabyte = 1024 * megabyte;
        private static readonly long terabyte = 1024 * gigabyte;

        /// <summary>
        /// Formats bytes to the next better unit
        /// </summary>
        /// <param name="bytes">Number of bytes to be formated</param>
        /// <returns>The formated result</returns>
        public static string ToByteString(long bytes)
        {
            if (bytes > terabyte)
                return (bytes / terabyte).ToString("0.00 TB");
            else if (bytes > gigabyte) 
                return (bytes / (double)gigabyte).ToString("0.00 GB");
            else if (bytes > megabyte) 
                return (bytes / (double)megabyte).ToString("0.00 MB");
            else if (bytes > kilobyte) 
                return (bytes / (double)kilobyte).ToString("0.00 KB");
            else 
                return bytes + " Bytes";
        }
    }
}
